package com.rongshu.view.home.presenter;

import android.content.Context;

import com.rongshu.api.RetorfitService;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.discovery.bean.ArticleDetailsResponse;
import com.rongshu.view.home.contract.SingleArticleDetailsView;
import com.rongshu.view.home.entity.ArticleAttribute;
import com.rongshu.view.home.entity.ArticleItem;
import com.rongshu.view.home.entity.ArticleLike;
import com.rongshu.view.home.entity.UserCollect;

import rx.Subscriber;
import rx.functions.Func1;

public class SingleArticleDetailsPresenter extends BasePresenter<SingleArticleDetailsView> {

    public SingleArticleDetailsPresenter(Context context) {
        super(context);
    }

    /**
     * 单个文章详情
     */
    public void getOnePostDetail(int user_id, int contribute_id,
                                 int type, int topic_id, String sign) {
        RetorfitService.getOnePostDetail(user_id, contribute_id, type, topic_id, sign)
                .map(new Func1<ArticleDetailsResponse, ArticleItem>() {
                    @Override
                    public ArticleItem call(ArticleDetailsResponse articleDetailsResponse) {
                        ArticleItem articleItem = articleDetailsResponse.getResponse();
                        if (articleItem != null) {
                            int serialize_id = articleItem.getSerialize_id();
                            if (serialize_id > 0) {
                                articleItem.setSerial(true);
                            }
                        }
                        return articleItem;
                    }
                })
                .compose(getView().<ArticleItem>bindToLife())
                .subscribe(new Subscriber<ArticleItem>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ArticleItem articleItem) {
                        if (getView() != null) {
                            getView().onOnePostDetail(articleItem);
                        }
                    }
                });
    }

    /**
     * 连载翻页
     */
    public void getFEPageTurn(int userId, int serialize_id, int sort, int type, String sign) {
        RetorfitService.getFEPageTurn(userId, serialize_id, sort, type, sign)
                .map(new Func1<ArticleDetailsResponse, ArticleItem>() {
                    @Override
                    public ArticleItem call(ArticleDetailsResponse response) {
                        ArticleItem articleItem = response.getResponse();
                        if (articleItem != null) {
                            int serialize_id = articleItem.getSerialize_id();
                            if (serialize_id > 0) {
                                articleItem.setSerial(true);
                            }
                        }
                        return articleItem;
                    }
                })
                .subscribe(new Subscriber<ArticleItem>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ArticleItem articleItem) {
                        if (getView() != null) {
                            getView().onOnePostDetail(articleItem);
                        }
                    }
                });
    }

    /**
     * 浏览
     */
    public void getbrowse(int user_id, int contribute_id, int type, String code, String sign) {
        RetorfitService.getbrowse(user_id, contribute_id, type, code, sign)
                .subscribe(new Subscriber<INetBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(INetBean bean) {
                        if (getView() != null) {
                            getView().onBrowseArticle(bean);
                        }
                    }
                });
    }

    /**
     * 点击收藏/点击取消
     */
    public void getIsCollect(int user_id, int type, int content_id, String sign) {
        RetorfitService.getIsCollect(user_id, type, content_id, sign)
                .subscribe(new Subscriber<INetBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(INetBean iNetBean) {
                        if (getView() != null) {
                            getView().onIsCollect(iNetBean);
                        }
                    }
                });
    }

    /**
     * 获取我是否收藏
     */
    public void getIsShouChang(int user_id, int contribute_id, int type, String sign) {
        RetorfitService.getIsShouChang(user_id, contribute_id, type, sign)
                .subscribe(new Subscriber<UserCollect>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(UserCollect userCollect) {
                        if (getView() != null) {
                            getView().onIsConcern(userCollect);
                        }
                    }
                });
    }

    /**
     * 获取文章属性
     */
    public void getArticleAttribute(int user_id, int contribute_id, String sign) {
        RetorfitService.getArticleAttribute(user_id, contribute_id, sign)
                .compose(getView().<ArticleAttribute>bindToLife())
                .subscribe(new Subscriber<ArticleAttribute>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ArticleAttribute articleAttribute) {
                        if (getView() != null) {
                            getView().onArticleAttribute(articleAttribute);
                        }
                    }
                });
    }

    /**
     * 文章点赞/帖子点赞
     */
    public void getlike(int id, int user_id, String type, String sign) {
        RetorfitService.getlike(id, user_id, type, sign)
                .compose(getView().<ArticleLike>bindToLife())
                .subscribe(new Subscriber<ArticleLike>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ArticleLike articleLike) {
                        if (getView() != null) {
                            getView().onArticleLikeOrOffLike(articleLike, true);
                        }
                    }
                });
    }

    /**
     * 取消文章点赞
     */
    public void getOfflike(int user_id, String type, int contribute_id, String sign) {
        RetorfitService.getOfflike(user_id, type, contribute_id, sign)
                .compose(getView().<ArticleLike>bindToLife())
                .subscribe(new Subscriber<ArticleLike>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ArticleLike articleLike) {
                        if (getView() != null) {
                            getView().onArticleLikeOrOffLike(articleLike, false);
                        }
                    }
                });
    }
}
